﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TradeCore.Model;
using TradeCore.Model.Entity;
using WAF.EntityFramework;

namespace TradeCore.Trade
{
    /// <summary>
    /// 交易管理类
    /// </summary>
    public class TradeManager
    {
        /// <summary>
        /// CTP交易接口
        /// </summary>
        CtpTrade ctpTrade;

        /// <summary>
        /// 单例模式实例对象
        /// </summary>
        private static TradeManager instance;


        /******************************** Public ********************************/

        /// <summary>
        /// 取得交易管理类实例
        /// </summary>
        /// <returns>交易管理类实例</returns>
        public static TradeManager GetInstance()
        {
            if (instance == null)
                instance = new TradeManager();

            return instance;
        }

        /// <summary>
        /// 买入开仓
        /// </summary>
        /// <param name="market">市场</param>
        /// <param name="policy">策略</param>
        /// <param name="code">代码</param>
        /// <param name="price">价格</param>
        /// <param name="volume">数量</param>
        public void Buy(MarketType market, string policy, string code, double price, int volume)
        {
            trade(market, policy, code, DirectionType.Buy, price, volume);
        }

        /// <summary>
        /// 卖出开仓
        /// </summary>
        /// <param name="market">市场</param>
        /// <param name="policy">策略</param>
        /// <param name="code">代码</param>
        /// <param name="price">价格</param>
        /// <param name="volume">数量</param>
        public void Sell(MarketType market, string policy, string code, double price, int volume)
        {
            trade(market, policy, code, DirectionType.Sell, price, volume);
        }

        /// <summary>
        /// 平仓
        /// </summary>
        /// <param name="market">市场</param>
        /// <param name="policy">策略</param>
        /// <param name="code">代码</param>
        /// <param name="price">价格</param>
        /// <param name="volume">数量</param>
        public void Close(MarketType market, string policy, string code, double price, int volume)
        {
            trade(market, policy, code, DirectionType.Close, price, volume);
        }

        /// <summary>
        /// 是否持有仓位
        /// </summary>
        /// <param name="market">市场</param>
        /// <param name="policy">策略名称</param>
        /// <param name="code">代码</param>
        /// <param name="direction">方向</param>
        /// <returns>如果持有仓位则返回True，否则返回False</returns>
        public bool HavePosition(MarketType market, string policy, string code, DirectionType direction)
        {
            return true;
        }


        /******************************** private ********************************/

        /// <summary>
        /// 私有构造函数，防止显示实例化
        /// </summary>
        private TradeManager()
        {
            ctpTrade = new CtpTrade();
        }

        /// <summary>
        /// 交易指令
        /// </summary>
        /// <param name="market">市场</param>
        /// <param name="policy">策略</param>
        /// <param name="code">代码</param>
        /// <param name="direction">方向</param>
        /// <param name="price">价格</param>
        /// <param name="volume">数量</param>
        private void trade(MarketType market, string policy, string code, DirectionType direction, double price, int volume)
        {
            //产生交易记录
            TradeRecord tradeRecord = new TradeRecord();

            tradeRecord.TradeTime = DateTime.Now.ToString();
            tradeRecord.Policy = policy;
            tradeRecord.Market = market.ToString();
            tradeRecord.Code = code;
            tradeRecord.Direction = direction.ToString();
            tradeRecord.Volume = volume;
            tradeRecord.Price = price;

            RecordTrade(tradeRecord);
            
            //期货接口
            if (market == MarketType.Futures)
            {
                if (direction == DirectionType.Buy)
                    ctpTrade.Buy(policy, code, price, volume);
                else
                    ctpTrade.Sell(policy, code, price, volume);
            }

            System.Media.SystemSounds.Beep.Play();
        }

        /// <summary>
        /// 获取持仓字典的Key
        /// </summary>
        /// <param name="market">市场</param>
        /// <param name="policy">策略名称</param>
        /// <param name="code">代码</param>
        /// <param name="direction">方向</param>
        /// <returns>持仓字典的Key</returns>
        private string getKey(MarketType market, string policy, string code, DirectionType direction)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(market.ToString()).Append("_")
              .Append(policy).Append("_")
              .Append(code).Append("_")
              .Append(direction.ToString());

            return sb.ToString();
        }

        /// <summary>
        /// 记录交易
        /// </summary>
        /// <param name="tradeRecord">交易记录</param>
        private void RecordTrade(TradeRecord tradeRecord)
        {
            EntityAction.Insert(tradeRecord);
        }
    }
}
